import {computed, observable} from 'mobx'

class RegisterStore {
  // 表单数据
  @observable formData = {
    name: '',
    id_card: '',
    parent_name: '',
    parent_phone: '',
    student_type: ''
  }

  // 学校信息
  @observable schoolName = ''
  @observable examYear = ''
  @observable courseInfo = []  // 确保初始化为空数组
  @observable agreementNo = ''
  @observable loading = false
  @observable paymentInfo = {}
  @observable paidAmount = 0
  @observable remainAmount = 0

  // 页面状态控制
  @observable currentStep = 'form'  // 'form' | 'agreement' | 'payment'

  @observable openid = ''  // 存储用户openid

  @observable orderid = '' // 存储订单id

  @computed get courseTotalPrice() {
    return this.courseInfo.reduce((total, course) => {
      return total + (course.price || 0)
    }, 0)
  }

  // 清空数据
  clear() {
    this.formData = {
      name: '',
      id_card: '',
      parent_name: '',
      parent_phone: '',
      student_type: ''
    }
    this.courseInfo = []  // 确保重置为空数组
    this.schoolName = ''
    this.examYear = ''
    this.currentStep = 'form'
  }

  // 设置课程信息，确保是数组
  setCourseInfo(info) {
    console.log('info', info)
    this.courseInfo = Array.isArray(info) ? info : []
  }

  setOpenid(openid) {
    this.openid = openid
  }

  // 获取课程信息，确保返回数组
  getCourseInfo() {
    return Array.isArray(this.courseInfo) ? this.courseInfo : []
  }

  // // 获取总金额
  // get totalAmount() {
  //   return this.courses.reduce((sum, course) => sum + course.price, 0);
  // }

  // // 获取课程名称列表
  // get courseNames() {
  //   return this.courses.map(course => course.name);
  // }
}

export default new RegisterStore()